package com.wwh.furns.web;

import com.wwh.furns.entity.Cart;
import com.wwh.furns.entity.Member;
import com.wwh.furns.entity.Order;
import com.wwh.furns.entity.OrderItem;
import com.wwh.furns.service.OrderService;
import com.wwh.furns.service.impl.OrderServiceImpl;
import com.wwh.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author Albert
 * @date 2024/7/31-22:16
 * @describe
 */
public class OrderServlet extends BasicServlet{
    OrderService orderService = new OrderServiceImpl();

    /**
     * 读取订单小项
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    public void readOrderItem(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String orderId = req.getParameter("orderId");
        List<OrderItem> orderItems = orderService.readOrderItemByOrderId(orderId);
        int count = 0;
        BigDecimal totalPrice = new BigDecimal(0);
        for (OrderItem orderItem : orderItems) {
            count += orderItem.getCount();
            totalPrice = totalPrice.add(orderItem.getTotalPrice());
        }
        req.getSession().setAttribute("orderItems", orderItems);
        req.setAttribute("count", count);
        req.setAttribute("totalPrice", totalPrice);
        req.getRequestDispatcher("/views/order/order_detail.jsp").forward(req, resp);
    }

    /**
     * 读取订单，根据用户id返回订单
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    public void readOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //获取当前登录的memberId
        Member member = (Member) req.getSession().getAttribute("member");
        if(member == null){
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
            return;
        }

        int memberId = DataUtils.parseInt(req.getParameter("memberId"), 0);
        List<Order> orders = orderService.readOrder(memberId);
        req.getSession().setAttribute("orders", orders);
        req.getRequestDispatcher("/views/order/order.jsp").forward(req, resp);
    }

    /**
     * 保存订单
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    public void saveOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //业务代码
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart == null || cart.isEmpty()){
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
            return;
        }

        //获取当前登录的memberId
        Member member = (Member) req.getSession().getAttribute("member");
        if(member == null){
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
            return;
        }

        String orderId = orderService.saveOrder(cart, member.getId());
        req.getSession().setAttribute("orderId", orderId);
        resp.sendRedirect(req.getContextPath() + "/views/order/checkout.jsp");
    }


}
